<template>
  <div class="member">
    <el-form
      :inline="true"
      size="small"
      :model="memberSearch"
      class="demo-form-inline"
      ref="ruleForm"
    >
      <el-form-item prop="id">
        <el-input
          v-model.trim.number="memberSearch.id"
          clearable
          @clear="getMemberList"
          placeholder="会员ID"
        ></el-input>
      </el-form-item>
      <el-form-item prop="cardNum">
        <el-input
          v-model.trim="memberSearch.cardNum"
          placeholder="会员卡号"
        ></el-input>
      </el-form-item>
      <el-form-item prop="name">
        <el-input
          v-model.trim="memberSearch.name"
          placeholder="会员名字"
        ></el-input>
      </el-form-item>
      <el-form-item prop="payType">
        <el-select v-model.trim="memberSearch.payType" placeholder="支付类型">
          <el-option
            v-for="(item, index) in payType"
            :key="index"
            :label="item.name"
            :value="item.type"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="birthday">
        <el-date-picker
          v-model.trim="memberSearch.birthday"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchMember" icon="el-icon-search"
          >搜索</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="memberList" border style="width: 100%">
      <el-table-column type="index" label="序号" width="60"> </el-table-column>
      <el-table-column prop="cardNum" label="会员卡号"> </el-table-column>
      <el-table-column prop="name" label="会员名字" width="80">
      </el-table-column>
      <el-table-column prop="birthday" label="会员生日" width="130">
      </el-table-column>
      <el-table-column prop="phone" label="手机号码" width="130">
      </el-table-column>
      <el-table-column prop="integral" label="可用积分" width="100">
      </el-table-column>
      <el-table-column prop="money" label="开卡金额" width="80">
      </el-table-column>
      <el-table-column prop="payType" label="支付类型" width="80">
        <template slot-scope="scope">
          {{ scope.row.payType | filterPaytype }}
        </template>
      </el-table-column>
      <el-table-column prop="address" label="会员地址" width="250">
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button size="mini">编辑</el-button>
          <el-button size="mini" type="danger" @click="removeClick(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import memberApi from "../../api/member";
let payType = [
  {
    type: 0,
    name: "现金",
  },
  {
    type: 1,
    name: "微信",
  },
  {
    type: 2,
    name: "支付宝",
  },
  {
    type: 3,
    name: "银行卡",
  },
];
export default {
  data() {
    return {
      page: 1,
      size: 8,
      memberSearch: {
        id: "",
        cardNum: "",
        name: "",
        payType: "",
        birthday: "",
      },
      memberList: [],
      total: 0,
      payType: payType,
    };
  },
  mounted() {
    this.getMemberList();
  },
  filters: {
    filterPaytype(val) {
      let data = payType.find((item, index) => {
        return item.type == val;
      });
      return data.name;
    },
  },
  methods: {
    getMemberList() {
      memberApi
        .getMenberList(this.page, this.size, this.memberSearch)
        .then((response) => {
          console.log(response);
          if (response.data.flag) {
            this.memberList = response.data.data.rows;
            this.total = response.data.data.total;
          } else {
            this.$message.error(response.data.message);
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    handleSizeChange(val) {
      this.size = val;
      this.getMemberList();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getMemberList();
    },
    searchMember() {
      this.getMemberList();
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    removeClick(id){
        memberApi.removeMenber(id).then(response=>{
            // console.log(response);
            if(response.data.flag){
                this.getMemberList();
                this.$message.success(response.data.message);
            }else{
                this.$message.error(response.data.message);
            }
        }).catch(error=>{
            console.log(error);
        })
    }
  },
};
</script>

<style scoped >
.el-form {
  margin-top: 20px;
}
.el-pagination {
  margin-top: 8px;
}
</style>
